package week0501;

import java.util.Arrays;
import java.util.Scanner;


public class Main1 {

	private static Scanner in;

    public static void main(String[] args) {
        in = new Scanner(System.in);
        int n;

        while (true) {
            n = in.nextInt();
            Person[] personSortables = new Person[n];
            for (int i = 0; i < n; i++) {
                personSortables[i] = new Person(in.next(), in.nextInt());
            }
			Arrays.sort(personSortables,(o1,o2)->{
            	int k=o1.getName().compareTo(o2.getName());
            	if(k!=0)
            	{
            		return k;
            	}
            	else{
            		return o1.getAge()- o2.getAge();
            	}
            });
            
			for (Person personSortable : personSortables) {
                System.out.println(personSortable.toString());
            }
         
        }

    }
}

class Person{
	String name;
	int age;
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	
}